<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <form action="register">
        <div class="input-prepend restyle">
          <input v-model="user.mobile" type="text" placeholder="手机号" />
          <i class="iconfont icon-phone" />
        </div>
        <div class="input-prepend">
          <input v-model="user.password" type="password" placeholder="密码" />
          <i class="iconfont icon-password" />
        </div>
        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="登录"
            @click="submitLogin()"
          />
        </div>
      </form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li>
            <a
              id="weixin"
              class="weixin"
              href="http://localhost:9001/api/ucenter/wx/login"
              ><i class="iconfont icon-weixin"
            /></a>
          </li>
          <li>
            <a id="qq" class="qq" target="_blank" href="#"
              ><i class="iconfont icon-qq"
            /></a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";
import cookie from "js-cookie";
import loginApi from "~/api/login";

export default {
  layout: "sign",

  data() {
    return {
      user: {
        mobile: "",
        password: "",
      },
    };
  },

  methods: {
    // 登录
    submitLogin() {
      // 执行远程登录接口调用
      loginApi.submitLogin(this.user).then((response) => {
        if (response.success) {
          // 将jwt写入cookie
          cookie.set("guli_jwt_token", response.data, { domain: "localhost" });
          this.$message.success(response.message);
          if (document.referrer.indexOf("register") !== -1) {
            //如果上一页是注册页面，则跳转到网站的首页
            window.location.href = "/";
          } else {
            history.go(-1); //跳转到上一个刚刚访问的页面
          }
        } else {
          this.$message.error(response.message);
        }
      });
    },
  },
};
</script>
